import {
  getOrderByUserIdAPI
} from '../../../api/order'
Page({
  data: {
    userId: 0,
    orderList: [],
    order: {
      value: 'default',
      options: [{
        value: 'default',
        label: '最新订单',
      }, ],
    },
    sorter: {
      value: 'default',
      options: [{
          value: 'default',
          label: '默认排序',
        },
        {
          value: 'priceH',
          label: '价格从低到高',
        },
        {
          value: 'priceL',
          label: '价格从高到低',
        },
      ],
    },
  },
  async getOrderByUserId(userId) {
    let that = this
    await getOrderByUserIdAPI(userId).then(res => {
      if (res.code == 200) {
        that.setData({
          orderList: res.result
        })
      }
    })
  },
  jumpDetail(event) {
    const id = JSON.stringify(event.currentTarget.id);
    wx.navigateTo({
      url: `/pages/profile/order/orderDetail/orderDetail?id=${id}`,
    });
  },
  onChange(e) {
    this.setData({
      'order.value': e.detail.value,
    });
    if (e.detail.value == 'priceH') {
      let arrH = this.data.orderList.sort((a, b) => {
        return a.price - b.price
      })
      console.log(arrH);
      this.setData({
        orderList: arrH
      });
    } else if (e.detail.value == 'priceL') {
      let arrL = this.data.orderList.sort((a, b) => {
        return b.price - a.price
      })
      this.setData({
        orderList: arrL
      });
    } else {
      this.getOrderByUserId(this.data.userId)
    }
  },
  onLoad(options) {
    let user = wx.getStorageSync('user')
    if (user) {
      this.setData({
        userId: user.id,
      })
    }
    this.getOrderByUserId(this.data.userId)
  },
  onShow() {
    this.onLoad()
  },
})